package com.shide.businessuser.UI;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.shide.businessuser.R;
import com.shide.businessuser.Utils.MessageEvent;
import com.shide.businessuser.http.entity.GoodsType;
import com.shide.businessuser.http.entity.Roll;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import java.util.logging.LogRecord;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/*最底下增加了一个动态添加数据的按钮;自定义布局需要从确定键回传数据*/
public class CustomOptionPickerUtil {
    Context context;

    private OptionsPickerView pvCustomOptions;
    private List<GoodsType> mGoodsTypes;


    public CustomOptionPickerUtil(Context context,List<GoodsType> goodsTypes) {
        this.context = context;
       // getRollData();

        mGoodsTypes = goodsTypes;
    }





    public OptionsPickerView initCustomOption(final TextView tv_types ,TextView uint,TextView tv_typeId) {

        pvCustomOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置,接收数据;
                String tx =mGoodsTypes.get(options1).getPickerViewText();
                //String tx = rolls.get(options1).getPickerViewText(); //这里只有一级


               if (null != uint ){
                   uint.setText(mGoodsTypes.get(options1).getProductUnit());
               }
                tv_typeId.setText(mGoodsTypes.get(options1).getId());
                tv_types.setText(tx);

                MessageEvent messageEvent1 = new MessageEvent();
                messageEvent1.setCode(MessageEvent.CHECK_DEFUALT_TPYEPIC);
                EventBus.getDefault().post(messageEvent1);


                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setCode(MessageEvent.RECORD_REFRESH);
                EventBus.getDefault().post(messageEvent);


                //数据往哪传;
            }
        })       .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {//自定义
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomOptions.returnData();//点击确定返回选择的数据
                        pvCustomOptions.dismiss();
                    }
                });

                ivCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomOptions.dismiss();
                    }
                });

            }
        })
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .setSelectOptions(0)//设置默认选项;
                .setCyclic(false,false,false)//设置是否可以循环;
                .isDialog(false)//设置是否在页面中心弹出对话框;
                .setContentTextSize(20)//设置滚轮文字大小
                .setLineSpacingMultiplier(2.5f)//设置行间距
                .setTextColorCenter(context.getResources().getColor(R.color.blue))//选中的字体的颜色;
                .setDividerColor(context.getResources().getColor(R.color.blue))//设置分割线的颜色
                .build();
        pvCustomOptions.setPicker(mGoodsTypes);//添加数据
        return pvCustomOptions;
    }


    /*以下数据源,不应该写死应该是灵活配置的文件,或者从后台进行传,请求一次*/

    private ArrayList<Roll> rolls = new ArrayList<>();
    private void  getRollData() {
        Roll roll1= new Roll();
        roll1.setId(1); roll1.setRoll("税务");

        Roll roll2= new Roll();
        roll2.setId(2); roll2.setRoll("治超");

        Roll roll3= new Roll();
        roll3.setId(3); roll3.setRoll("能源");


        rolls.add(roll1);rolls.add(roll2);rolls.add(roll3);
    }
    
}
